package com.gitpractice.liwushuo.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.gitpractice.liwushuo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class HomeFragment extends Fragment {

    @InjectView(R.id.rl_homepage)
    RelativeLayout rlHomepage;
    @InjectView(R.id.tab_view)
    TabLayout tabView;
    @InjectView(R.id.vp_conten)
    ViewPager vpConten;
    private List<Fragment> mFragments;
    private HomeAdapter homeAdapter;
    private String[] mtitls = {"精选", "海淘", "送女票", "创意生活",
                                "送基友", "送爸妈", "送同事", "送宝贝", "设计感",
                                "文艺风", "奇葩搞怪", "科技范", "萌萌哒"};

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.inject(this, view);
        mFragments = new ArrayList<>();
        return view;
    }

    @Override
    public void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        for (int i=0;i<mtitls.length;i++){
            mFragments.add(new ItemFragment().newInstance(i));
        }

        homeAdapter = new HomeAdapter(getChildFragmentManager());
        vpConten.setAdapter(homeAdapter);
        tabView.setupWithViewPager(vpConten);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    class HomeAdapter extends FragmentPagerAdapter {

        public HomeAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mtitls[position];
        }
    }
}
